#include<16f877a.h>
#device ADC=10
#include<stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
//#use delay(clock=20000000)
//#use delay(clock=11592000)
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

/*======================================================main function=================================================*/
void main()
{
   unsigned char g=0,a[3];
   unsigned int temp_adc;
   port_b_pullups(true);
   setup_port_a(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);

if(!input(PIN_B2))
{


while(1)
{
    set_adc_channel(0);
    temp_adc=Read_ADC();

    delay_us(20);
    printf("Sensor Value=%d\n\r",temp_adc);
    delay_ms(100);

           a[g++]=temp_adc;
         if(g>=3)
         {
         temp_adc=(a[0]+a[1]+a[2])/3;
      	     if((temp_adc>=74)&&(temp_adc<=76))
                 {
      		          output_c(0x07);//metal oe human ob wall
                      delay_ms(2000);
                      output_c(0x0f);
                      delay_ms(5000);
                      g=0;
                 }
              else if((temp_adc>76)&&(temp_adc<=80))
                 {
      		          output_c(0x0D);//wood
                      delay_ms(2000);
                      output_c(0x0f);
                      delay_ms(5000);
                      g=0;
                 }
              else
                 {
                      	output_c(0x0f);g=0;
                 }
         }
   }
}
else
{

while(1)
{
    set_adc_channel(0);
    temp_adc=Read_ADC();
    printf("Sensor Value=%d\n\r",temp_adc);
    delay_us(20);
    delay_ms(100);

           a[g++]=temp_adc;
         if(g>=3)
         {
         temp_adc=(a[0]+a[1]+a[2])/3;
      	     if((temp_adc>=149)&&(temp_adc<=151))
                 {
      		          output_c(0x07);//metal oe human ob wall
                      delay_ms(2000);
                      output_c(0x0f);
                      delay_ms(5000);
                      g=0;
                 }
              else if((temp_adc>151)&&(temp_adc<=157))
                 {
      		          output_c(0x0D);//wood
                      delay_ms(2000);
                      output_c(0x0f);
                      delay_ms(5000);
                      g=0;
                 }
              else if((temp_adc>158)&&(temp_adc<=165))
                 {
      		          output_c(0x0E);//Human
                      delay_ms(2000);
                      output_c(0x0f);
                      delay_ms(5000);
                      g=0;
                 }
              else
                 {
                      	output_c(0x0f);g=0;
                 }
         }
   }
}

}


